#coding=utf-8
from uliweb import expose

#可以省略直接引用
from uliweb import request,functions,json
from sqlalchemy import and_,or_,desc,asc

def __begin__():
    """
    用户验证
    """
    from uliweb import functions
    return functions.require_login()

@expose('/notifypost')
class PostData(object):

    
    def __init__(self):
        from uliweb.orm import get_model
        self.usermodel = get_model('user')
        self.notifymodel = get_model('notify')
  
    def notify(self):
        """        
        所有通知的数据,用于bootstrap-table
        """
        
        user = request.user
        
        #禁止非管理员查看
        if not user.is_superuser:
            error("页面不存在！")
        
        #得到偏移量
        pageIndex= request.GET.get('offset',0)
        #得到查询个数
        pageSize = request.GET.get('limit',20)
        
            
        #得到排序字段
        sort = request.GET.get('sort','id')
        #得到排序规则
        order_by_type = request.GET.get('order','asc')
        
        #使用sqlchemy自带asc方法，uliweb版本  self.notifymodel.c['id'].asc()或self.notifymodel.c.id.asc()
        if order_by_type == 'asc':
            #升序
            #order_by = asc('id')
            order_by = self.notifymodel.c[sort].asc()
        else:
            #降序
            #order_by = desc ('id')
            order_by = self.notifymodel.c[sort].desc()
        
        data = {}
        data['rows'] = []


        #得到总数
        data['total'] = self.notifymodel.all().count()
        
        #分页获取数据
        ormdata = self.notifymodel.all().order_by(order_by).limit(pageSize).offset(pageIndex)


        for i in ormdata:
                
            #删除用户关联丢失处理
            try:
                senduser = i.senduser_id.nickname
            except:
                senduser = "用户已删除"

            #删除用户关联丢失处理
            try:
                #receiveuser_id为None，基本为注册，接收者为管理员
                if i.receiveuser_id == None:
                    receiveuser = "管理员"
                else:
                    receiveuser = i.receiveuser_id.nickname
            except:
                receiveuser = "用户已删除"
                
            if i.notify_type == 1:
                notify_type = "注册通知"
            else:
                notify_type = "其它通知"
            
            data['rows'].append({"id":i.id,
                         "senduser_id":senduser,
                         "receiveuser_id":receiveuser,
                         "content":i.content,
                         "notify_type":notify_type,
                         "send_date":i.send_date
                         })
        #返回json串
        return json(data)

##############################################################################################
    def usernotify(self):
        """
        首页消息通知数量
        """
        
        _id = request.GET.get("id")
        
        user = functions.get_object(self.usermodel, _id, cache=True, use_local=True)
        
        if user.is_superuser:
            #管理员显示自己的和注册类型的
            key = or_(self.notifymodel.c.notify_type == 1,
                      self.notifymodel.c.receiveuser_id == user.id)
    
            ormdata_num = self.notifymodel.filter(key).filter(self.notifymodel.c.senduser_id.in_(self.usermodel.filter().fields('id'))).count()
    
        else:
            #非管理员显示自己相关的
            ormdata_num = self.notifymodel.filter(self.notifymodel.c.receiveuser_id == user.id).filter(self.notifymodel.c.senduser_id.in_(self.usermodel.filter().fields('id'))).count()
    
        return ormdata_num   